﻿#region 命名空间

using System;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.CMSGG
{
    /// <summary>
    ///     Admin_Ads_Add 的摘要说明。
    /// </summary>
    [Access("260001,260002")]
    public partial class Add : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["ID"] != null) //修改界面
                {
                    Edit_Load();
                }
                else //添加界面
                {
                    this.txtMaxDateTime.Value = DateTime.Now.AddMonths(1).ToString();
                }
            }
        }


        private void Edit_Load()
        {
            var model = new AdsManage().GetInfo(Request["ID"]);

            this.TtxtSiteName.Text = model.SiteName;
            this.TtxtUserName.Text = model.UserName;
            this.TtxtContact.Text = model.Contact;
            this.TtxtAd_Url.Text = model.AdUrl;
            this.TtxtUrl.Text = model.Url;
            this.NtxtWidth.Text = model.Width.ToString();
            this.NtxtHeight.Text = model.Height.ToString();
            this.ddlTarget.SelectedValue = model.Target;
            this.TtxtMaxShowTime.Text = model.MaxShowTime.ToString();
            this.TtxtMaxClick.Text = model.MaxClick.ToString();
            this.txtMaxDateTime.Value = model.MaxDateTime.ToString("yyyy-MM-dd");

            this.ddlShowType.SelectedValue = model.ShowType.ToString();
            this.TtxtRemark.Text = model.Remark;

            this.TtxtNote.Text = model.Note;
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var model = new Advert();
            if (Request["ID"] == null)
            {
                model.Id = 0;
            }
            else
            {
                model.Id = int.Parse(Request["ID"]);
            }
            model.SiteName = this.TtxtSiteName.Text;
            model.UserName = this.TtxtUserName.Text;
            model.Contact = this.TtxtContact.Text;
            model.AdUrl = this.TtxtAd_Url.Text;
            model.Url = this.TtxtUrl.Text;
            model.Width = int.Parse(this.NtxtWidth.Text);
            model.Height = int.Parse(this.NtxtHeight.Text);
            model.Target = this.ddlTarget.SelectedValue;
            model.MaxShowTime = int.Parse(this.TtxtMaxShowTime.Text);
            model.MaxClick = int.Parse(this.TtxtMaxClick.Text);
            model.MaxDateTime = DateTime.Parse(this.txtMaxDateTime.Value);

            model.ShowType = int.Parse(this.ddlShowType.SelectedValue);
            model.Remark = this.TtxtRemark.Text;
            model.Note = this.TtxtNote.Text;

            var res = PolicyInjection.Create<AdsManage>().Save(model);
            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this);
            }
            else
            {
                MsgBox.Message("操作成功。", "List.aspx", this);
            }
        }


        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.HasFile)
                try
                {
                    var config = CMSConfig.Instance();
                    this.FileUpload1.SaveAs(Server.MapPath(config.UpLoadFolder + "adFile/") + this.FileUpload1.FileName);
                    this.TtxtAd_Url.Text = config.UpLoadFolder + "adFile/" + this.FileUpload1.FileName;
                    this.TtxtAd_Url.Text = this.TtxtAd_Url.Text.Replace(@"\", "/");
                }
                catch (Exception ex)
                {
                    MsgBox.Message(ex.Message, this);
                }
            else
            {
                MsgBox.Message("请选择文件！", this);
            }
        }
    }
}